package com.hanko.web;

import com.hanko.repository.Person;
import com.hanko.repository.PersonRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @desc: Using SpringBoot+Spring Data Repositories
 * @author: luodengxiong
 * @date: 2020-12-22 9:17:17
 **/
@Api(tags = "CrudRepository 操作方式")
@RestController
@RequestMapping("/CrudRepository")
public class CrudRepositoryController {

    @Autowired
    PersonRepository personRepository;

    @ApiOperation(value = "创建文档")
    @GetMapping("create")
    public String create() throws IOException {
        String id = "104";
        Person person = Person.builder()
                .id(id)
                .firstname("hanko "+ id)
                .lastname("zhou")
                .build();
        Person result = personRepository.save(person);
        return result.toString();
    }

    @ApiOperation(value = "通过Lastname精确查询")
    @GetMapping("findByLastname")
    public String findByLastname() throws IOException {
        List<Person> result = personRepository.findByLastname("zhou");
        return result.toString();
    }

    /**
     * 通过Firstname模糊查询
     * 以Like Contains Containing结尾即模糊查询
     * Like 支持通配符  类似  han  会自动变为  han*
     * Like类似StartingWith
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "通过Firstname模糊查询 Like")
    @GetMapping("findByFirstnameLike")
    public String findByFirstnameLike() throws IOException {
        List<Person> result = personRepository.findByFirstnameLike("han");
        return result.toString();
    }

    /**
     * 通过Firstname模糊查询
     * 以Like Contains Containing结尾即模糊查询
     * Contains 支持通配符  类似  han  会自动变为  *ank*
     *
     */
    @ApiOperation(value = "通过Firstname模糊查询 Contains")
    @GetMapping("findByFirstnameContains")
    public String findByFirstnameContains() throws IOException {
        List<Person> result = personRepository.findByFirstnameContains("ank");
        return result.toString();
    }

    @ApiOperation(value = "高亮显示,使用@Highlight注解")
    @GetMapping("queryByFirstname")
    public String queryByFirstname() throws IOException {
        List<SearchHit<Person>> result = personRepository.findByFirstname("hanko");
        result.forEach(res-> {
            Map<String, List<String>> highlightFields = res.getHighlightFields();
            List<String> highlight = highlightFields.get("firstname");
            highlight.forEach(h-> System.out.println(h));
        });
        return result.toString();
    }

    @ApiOperation(value = "通过request脚本方式")
    @GetMapping("queryByScript")
    public String queryByScript() throws IOException {
        List<Person> result = personRepository.queryByScript("hanko");
        return result.toString();
    }

}
